/**
 * This file is part of Megasquirt Logger for PalmOS.
 * Copyright (C) 2011 Andrew Beard
 *
 * Megasquirt Logger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Megasquirt Logger is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Megasquirt Logger.  If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include "Timer.h"
#include <TimeMgr.h>

/**
 * Constructor for the Timer class.
 *
 * @param[in] nPeriod The timer period in milliseconds.
 */
Timer::Timer( UInt32 nPeriod ) :
    m_nStartTicks( 0 ),
    m_bRunning( false ),
    m_bExpired( false )
{
    m_nPeriodInTicks = MillisecondsToTicks( nPeriod );
}

/**
 * Destructor for the Timer class.
 */
Timer::~Timer()
{

}

/**
 * Starts running a one-shot timer at the period specified in the constructor.
 *   If the timer is currently running this restarts the timer safely.
 */
void Timer::StartTimer( void )
{
    m_nStartTicks = TimGetTicks();
    m_bExpired    = false;
    m_bRunning    = true;

    EvtSetNullEventTick( m_nStartTicks + m_nPeriodInTicks );
}

/**
 * Stops running the time.  This is safe to call even if the timer is currently stopped.
 */
void Timer::StopTimer( void )
{
    m_bRunning = false;
}

/**
 * Checks to see if the timer has expired.  This only updates the internal
 *   object state and does not return a value based on timer expiration.  To
 *   see if the timer has expired call IsExpired() after executing this method.
 *
 * @see IsExpired
 */
void Timer::CheckTimer( void )
{
    if ( m_bRunning &&
            !m_bExpired )
    {
        UInt32 nCurrentTime = TimGetTicks();
        if ( TickDifference(m_nStartTicks, nCurrentTime) >= m_nPeriodInTicks )
        {
            m_bExpired = true;
        }
    }
}

/**
 * Returns the status of the timer expiration.
 *
 * @return true if the timer has expired, false otherwise.
 */
bool Timer::IsExpired( void ) const
{
    return m_bExpired;
}

/**
 * Static function for computing the difference between two tick values.
 *   At the moment this function does not work if the tick value has rolled
 *   over, but it may be updated to support this case in the future.
 *
 * @param[in] nStartTime The first cronological time in ticks.
 * @param[in] nEndTime   The seconf cronological time in ticks.
 *
 * @return The time elapsed between the two times in ticks.
 */
UInt32 Timer::TickDifference( UInt32 nStartTime,
                              UInt32 nEndTime )
{
    return nEndTime - nStartTime;
}

/**
 * Static function for converting from a time in milliseconds to a time in
 *   ticks.
 *
 * @param[in] A time in milliseconds.
 *
 * @return The corresponding time in ticks.  Due to the integer math used this
 *           function is not entirely acurate if the number of ticks per second
 *           for the device being used is not a factor of 1000.
 */
UInt32 Timer::MillisecondsToTicks( UInt32 nMilliseconds )
{
    UInt16 nMillisecondsPerTick = 0;

    nMillisecondsPerTick = 1000 / SysTicksPerSecond();
    return ( nMilliseconds / nMillisecondsPerTick );
}